from django.db import models
from itsdangerous import TimedJSONWebSignatureSerializer as TJSWSerializer
from django.contrib.auth.models import AbstractUser
from django.conf import  settings


class User(AbstractUser):
    mobile = models.CharField(max_length=11,unique=True,verbose_name="手机号")
    email_active = models.BooleanField(default=False,verbose_name="邮箱激活状态")

    class Meta:
        db_table  = "tb_users"

    def __str__(self):
        return self.username


    #生成验证的链接
    def generate_verify_url(self):
        #1,生成数据
        token ={
            "user_id":self.id,"email":self.email
        }
        #2.1加密数据
        serializer = TJSWSerializer(secret_key=settings.SECRET_KEY,expires_in=300)
        token = serializer.dumps(token)
        #2.拼接到url地址中
        verify_url = "http://www.meiduo.site:8080/success_verify_email.html?token=%s" % token.decode()
        return verify_url
